package cn.edu.nju.askinginhand.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import cn.edu.nju.askinginhand.util.JSONParser;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import static cn.edu.nju.askinginhand.constant.QuesType.*;
/**
 * 
 * @author ZhangZu
 * 
 */
public class PictureQuesListActivity extends Activity {
	
	private Button quesBtn = null;
	private Button searchBtn = null;
	private Spinner firstSpinner = null;
	private Spinner secondSpinner = null;
	private ListView quesList = null;
	List<Map<String, Object>> list;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.picture_ques_list);
		
		quesBtn = (Button)findViewById(R.id.quesBtn);
		searchBtn = (Button)findViewById(R.id.searchBtn);
		firstSpinner = (Spinner) findViewById(R.id.firstSpinner);
		secondSpinner = (Spinner) findViewById(R.id.secondSpinner);
		quesList = (ListView) findViewById(R.id.quesList);
		
		initQuesBtn();
		initSearchBtn();
		initSpinner();
		initQuesList();
	}
	


	public void initQuesBtn() {
		quesBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				startActivity(new Intent(PictureQuesListActivity.this,
						PictureQuesActivity.class));
			}
		});
	}
	
	private void initSearchBtn() {
		searchBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				initQuesList();
			}
		});		
	}
	
	public void initSpinner() {
		ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, R.layout.questype_spinner_item,
				R.id.spinnerText, pic_class1);

		firstSpinner.setAdapter(adapter1);
		firstSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long id) {
				ArrayAdapter<String> adapter2 = null;
				if (position == 0) {
					secondSpinner.setVisibility(View.INVISIBLE);
				} else {
					secondSpinner.setVisibility(View.VISIBLE);
					adapter2 = new ArrayAdapter<String>(PictureQuesListActivity.this,
							R.layout.questype_spinner_item, R.id.spinnerText,
							pic_class2[position]);
					secondSpinner.setAdapter(adapter2);
				}
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				secondSpinner.setVisibility(View.INVISIBLE);
			}
		});
	}
	public void initQuesList() {
		SimpleAdapter adapter = new SimpleAdapter(this, getData(),
				R.layout.ques_list_item, new String[] { "quesImage",
						"quesTitle", "quesTime" }, new int[] { R.id.typeImage,
						R.id.titleText, R.id.timeText });
		quesList.setAdapter(adapter);
	}
	private List<Map<String, Object>> getData() {
/*		for (int i = 14; i >= 0; i--) {
			Map<String, Object> news = new HashMap<String, Object>();
			news.put("typeImage", R.drawable.q);
			news.put("timeText", i + "");
			news.put("titleText", "问题" + i);
			list.add(news);
		}*/
		
		//getType
		String type = firstSpinner.getSelectedItem().toString();
		if(secondSpinner.getVisibility()==View.VISIBLE&&secondSpinner.getSelectedItem()!=null)
			type = type+"-"+secondSpinner.getSelectedItem().toString();
		
		List <NameValuePair> params = new ArrayList <NameValuePair>();
		
		params.add(new BasicNameValuePair("status", 0+""));
		params.add(new BasicNameValuePair("type", type));

		String raw = JSONParser.getOut("PicQuesFindAction"+LoginActivity.filter,params);
		String quesListStr = JSONParser.removeRoot(raw, "result");
		List<Map<String, String>> serverList = JSONParser.getList(quesListStr);
		list = new ArrayList<Map<String, Object>>();

		for(int i = 0 ; i < serverList.size();i++) {
			Map<String, String> serverQues = serverList.get(i);
			Map<String, Object> ques = new HashMap<String, Object>();
			ques.put("quesImage", R.drawable.q);
			ques.put("quesTitle",serverQues.get("title"));
			ques.put("quesTime",serverQues.get("time"));
			ques.put("quesId",serverQues.get("id"));//replace i by Real Id here
			list.add(ques);
		}
		return list;
	}
}
